#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;
const int maxa = 1e6 + 10;
int n;
int r,b;
ll a[maxn],ans;
int sum[105][105];//
ll ll_max(int x,int y)
{
	return x > y ? x : y;
}
void dfs(int pos,ll sum)
{
	if(pos > n)
	{
		ans = ll_max(ans,sum);
		return;
	}
	//red
	int t = r;
	r = pos;
	if(r != -1 && a[pos] == a[t]) dfs(pos + 1,sum + a[pos]);
	else dfs(pos + 1,sum);
	r = t;
	//blue
	t = b;
	b = pos;
	if(b != -1 && a[pos] == a[t]) dfs(pos + 1,sum + a[pos]);
	else dfs(pos + 1,sum);
	b = t;
}
void solve()
{
	cin >> n;
	for(int i = 1;i <= n;i++)
	{
		cin >> a[i];
	}
	if(n <= 15)//20pts
	{
		ans = -1e9;
		r = b = -1;
		dfs(1,0ll);
		cout << ans << endl;
		return;
	}
	if(n <= 100)//15pts
	{
		for(int j = 0;j <= 100;j++)
		{
			sum[0][j] = 0;
		}	
		for(int i = 1;i <= n;i++)
		{
			for(int j = 0;j <= 100;j++)
			{
				sum[i][j] = sum[i - 1][j];
			}
			sum[i][a[i]]++;
		}
		ll ans = 0;
		r = 1,b = 2;
		while(a[r] == a[b])
		{
			ans += a[b];
			b++;
			r++;
		}
		for(int i = b + 1;i <= n;i++)
		{
			if(a[i] == a[r])
			{
				ans += a[i];
				r = i;
				continue;
			}
			if(a[i] == a[b])
			{
				ans += a[i];
				b = i;
				continue;
			}
			ll getr = (sum[n][a[r]] - sum[i][a[r]]) * a[r];
			ll getb = (sum[n][a[b]] - sum[i][a[b]]) * a[b];
			if(getr > getb)
			{
				b = i;
			}
			else r = i;
		}
		cout << ans << endl;
	}
}
int main()
{
	//all:35pts
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
	int t;
	cin >> t;
	while(t--)solve();
	return 0;
}
